﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Styx.WoWInternals;
using Styx.WoWInternals.WoWObjects;
using Styx.Logic.Combat;
using Styx.Helpers;
using System.Drawing;

namespace FelMaster
{
    public class AuraTranslator
    {
        private Dictionary<string, string> cache = new Dictionary<string, string>();

        private static AuraTranslator instance = new AuraTranslator();
        public static AuraTranslator Instance
        {
            get
            {
                return instance;
            }
        }

        private AuraTranslator() { }

        private ulong pulseCount = 0;
        public void Pulse(ulong modulo)
        {
            if (pulseCount++ % modulo != 0)
                return;

            Logging.WriteDebug("AuraTranslator: watching player and target auras...");
            ParseBuffs("player");
            ParseBuffs("target");
        }

        private void ParseBuffs(string unit)
        {
            var lua = "local y=''; for i=1,40 do local name, _, _, _, _, _, _, _, _, _, spellId = UnitAura(\""+unit+"\", i); if name then y=y..'|'..name..'|'..spellId; end end return y";
            var raw = Lua.GetReturnValues(lua)[0].Split('|');
            var GameAuras = new Dictionary<int, string>();
            for (var i=1; i<raw.Length; i+=2)
            {
                //Logging.Write(Color.PeachPuff, raw[i] + " -> " + raw[i + 1]);
                var localizedName = raw[i];
                var spellId = int.Parse(raw[i + 1]);
                try
                {
                    var spell = WoWSpell.FromId(spellId);
                    if (!cache.ContainsKey(spell.Name))
                    {
                        cache[spell.Name] = localizedName;
                        Logging.WriteDebug(Color.PeachPuff, "AuraTranslator: {0} is {1}.", spell.Name, localizedName);
                    }
                }
                catch { }
            }
        }

        public string LocalizeAuraName(string name)
        {
            try
            {
                return cache[name];
            }
            catch
            {
               throw new Exception("Failed to localize aura name: " + name);
            }
        }
    }
}
